package org.paybiz.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @Title: Fields.java
 * @Description: 
 * @author Nassir.wen  
 * @date 2010-11-25 下午04:44:18
 * @version V1.0  
 * @Company: MSD.
 * @Copyright Copyright (c) 2010
 */
@SuppressWarnings("unchecked")
public class Fields {
	public Fields() {
		field = new ArrayList();
	}

	public static Fields getInstance(Object o) {
		Fields f = new Fields();
		f.filter(o.getClass());
		return f;
	}

	public List getFields() {
		return field;
	}

	public Field[] getFieldsArray() {
		Field fields[] = new Field[field.size()];
		int i = 0;
		for (Iterator iterator = field.iterator(); iterator.hasNext();) {
			Field f = (Field) iterator.next();
			fields[i++] = f;
		}

		return fields;
	}

	private void filter(Class cls) {
		Field fieldlist[] = cls.getDeclaredFields();
		Field afield[];
		int j = (afield = fieldlist).length;
		for (int i = 0; i < j; i++) {
			Field f = afield[i];
			int foundMods = f.getModifiers();
			/*
			 * 16 final 8 static 2 private
			 */
			if (foundMods != 16 && foundMods != 8 && foundMods == 2)
				field.add(f);
		}

		Class c = cls.getSuperclass();
		if (c != null && c.getName().indexOf("java") == -1)
			filter(c);
	}

	private List field;

}
